package com.example.game

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.widget.EditText
import android.widget.Button

class EditInfoActivity : AppCompatActivity() {

    private lateinit var nameEditText: EditText
    private lateinit var studentIdEditText: EditText
    private lateinit var phoneEditText: EditText
    private lateinit var emailEditText: EditText
    private lateinit var genderEditText: EditText
    private lateinit var hobbyEditText: EditText
    private lateinit var saveButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_edit_info)

        // Set title
        title = getString(R.string.edit_info_title)

        // Initialize views
        initializeViews()

        // Load existing data
        loadExistingData()

        // Set up button click listener
        setupButtonListener()

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.input_fields_layout)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun initializeViews() {
        nameEditText = findViewById(R.id.name_edit_text)
        studentIdEditText = findViewById(R.id.student_id_edit_text)
        phoneEditText = findViewById(R.id.phone_edit_text)
        emailEditText = findViewById(R.id.email_edit_text)
        genderEditText = findViewById(R.id.gender_edit_text)
        hobbyEditText = findViewById(R.id.hobby_edit_text)
        saveButton = findViewById(R.id.save_button)
    }

    private fun loadExistingData() {
        val playerName = intent.getStringExtra(MainActivity.EXTRA_PLAYER_NAME) ?: ""
        val playerGender = intent.getStringExtra(MainActivity.EXTRA_PLAYER_GENDER) ?: ""
        val playerStudentId = intent.getStringExtra(MainActivity.EXTRA_PLAYER_STUDENT_ID) ?: ""
        val playerPhone = intent.getStringExtra(MainActivity.EXTRA_PLAYER_PHONE) ?: ""
        val playerEmail = intent.getStringExtra(MainActivity.EXTRA_PLAYER_EMAIL) ?: ""
        val playerHobby = intent.getStringExtra(MainActivity.EXTRA_PLAYER_HOBBY) ?: ""

        nameEditText.setText(playerName)
        studentIdEditText.setText(playerStudentId)
        phoneEditText.setText(playerPhone)
        emailEditText.setText(playerEmail)
        genderEditText.setText(playerGender)
        hobbyEditText.setText(playerHobby)
    }

    private fun setupButtonListener() {
        saveButton.setOnClickListener {
            val name = nameEditText.text.toString().trim()
            val studentId = studentIdEditText.text.toString().trim()
            val phone = phoneEditText.text.toString().trim()
            val email = emailEditText.text.toString().trim()
            val gender = genderEditText.text.toString().trim()
            val hobby = hobbyEditText.text.toString().trim()

            // Create result intent with updated data
            val resultIntent = Intent().apply {
                putExtra(MainActivity.EXTRA_PLAYER_NAME, name)
                putExtra(MainActivity.EXTRA_PLAYER_GENDER, gender)
                putExtra(MainActivity.EXTRA_PLAYER_STUDENT_ID, studentId)
                putExtra(MainActivity.EXTRA_PLAYER_PHONE, phone)
                putExtra(MainActivity.EXTRA_PLAYER_EMAIL, email)
                putExtra(MainActivity.EXTRA_PLAYER_HOBBY, hobby)
            }

            // Set result and finish activity
            setResult(Activity.RESULT_OK, resultIntent)
            finish()
        }
    }
}
